package net.jwtools.tmt.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

import net.jwtools.tmt.util.ExtendedControl;

import org.jdesktop.swingx.JXImageView;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class TerritoryMapViewer extends JXImageView {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected ResourceBundle bundle = ResourceBundle.getBundle(
			"net/jwtools/tmt/ui/messages", new ExtendedControl());
	BufferedImage overview;
	BufferedImage detail;
	private JButton toggleDetailOverview;
	private JButton zoomin;
	private JButton zoomout;

	public BufferedImage getOverview() {
		return overview;
	}

	public void setOverview(BufferedImage overview) {
			this.overview = overview;
			setImage(this.overview);
	}

	public BufferedImage getDetail() {
		return detail;
	}

	public void setDetail(BufferedImage detail) {
		this.detail = detail;
	}

	public TerritoryMapViewer() {
		zoomin = new JButton(bundle.getString("TerritoryMapViewer.zoomIn"));
		zoomin.setAction(getZoomInAction());
		zoomout = new JButton(bundle.getString("TerritoryMapViewer.zoomOut"));
		zoomout.setAction(getZoomOutAction());
		toggleDetailOverview = new JButton();

		toggleDetailOverview.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (detail == null)
					return;
				if (getImage().equals(overview)) {
					setImage(detail);
					toggleDetailOverview.setText(bundle
							.getString("TerritoryMapViewer.overView"));
				} else {
					setImage(overview);
					toggleDetailOverview.setText(bundle
							.getString("TerritoryMapViewer.detail"));
				}
			}
		});
		addMouseWheelListener(new MouseAdapter() {
			@Override
			public void mouseWheelMoved(MouseWheelEvent e) {
				if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
					if (e.getWheelRotation() < 0)
						getZoomOutAction().actionPerformed(
								new ActionEvent(this, 0, null));
				if (e.getWheelRotation() > 0)
					getZoomInAction().actionPerformed(
							new ActionEvent(this, 0, null));
				super.mouseWheelMoved(e);
			}
		});
		((FlowLayout) getLayout()).setAlignment(FlowLayout.RIGHT);
		add(zoomin);
		add(zoomout);
		add(toggleDetailOverview);
		toggleDetailOverview.setText(bundle
				.getString("TerritoryMapViewer.detail"));

	}

	public static void main(String[] args) throws IOException {
		TerritoryMapViewer test = new TerritoryMapViewer();
		test.setOverview(ImageIO.read(new File("images/1-101.png")));
		test.setDetail(ImageIO.read(new File("images/101.png")));
		JFrame f = new JFrame();
		test.setPreferredSize(new Dimension(500, 500));
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(new JScrollPane(test));
		f.getContentPane().add(test, "Last");
		f.pack();
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}
}
